package com.tjetc.controller;

import com.tjetc.common.JsonResult;
import com.tjetc.service.FavouriteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("favourite")
public class FavouriteController {

    @Autowired
    private FavouriteService favouriteService;
    @RequestMapping("user")
    public JsonResult userFavourite(@RequestParam(name ="folderId",required = false) Long folderId,
                                   @RequestParam(name = "name",required = false,defaultValue = "")String name,
                                    @RequestParam("userId")Long userId
                                 ) {
        return favouriteService.userFavourite(folderId, name,userId);
    }

    // 添加收藏
    @RequestMapping("add")
    public JsonResult addFavourite(@RequestParam(name ="folderId",required = false,defaultValue = "-1") Long folderId,
                                   @RequestParam("userId") Long userId,
                                   @RequestParam("type") Long type,
                                   @RequestParam("bindId") Long bindId) {
        return favouriteService.addFavourite(folderId, type, bindId,userId);
    }

    // 创建默认收藏夹
    @RequestMapping("init/{userId}")
    public JsonResult insertDefaultFolders(@PathVariable("userId") Long userId) {
        return favouriteService.insertDefaultFolders(userId);
    }

    // 添加新的收藏夹
    @RequestMapping("create")
    public JsonResult insertFolder(@RequestParam("userId") Long userId,
                                   @RequestParam("name") String name) {
        return favouriteService.insertFolder(userId, name);
    }

    // 删除收藏夹
    @RequestMapping("delete/{folderId}")
    public JsonResult deleteFolder(@PathVariable("folderId") Long folderId) {
        return favouriteService.deleteFolder(folderId);
    }

    // 删除收藏夹内的所有收藏项
    @RequestMapping("clear/{folderId}")
    public JsonResult deleteFavoritesByFolderId(@PathVariable("folderId") Long folderId) {
        return favouriteService.deleteFavoritesByFolderId(folderId);
    }

    // 删除指定收藏项
    @RequestMapping("remove")
    public JsonResult removeFavorite(@RequestParam(value = "folderId",required = false) Long folderId,
                                     @RequestParam("type") Long type,
                                     @RequestParam("bindId") Long bindId,
                                     @RequestParam(name = "userId",required = false) Long userId) {
        return favouriteService.removeFavorite(folderId, type, bindId,userId);
    }
    @RequestMapping("isfavourite")
    public JsonResult isfavourite(@RequestParam("userId") Long userId,
                                     @RequestParam("type") Long type,
                                     @RequestParam("bindId") Long bindId) {
        return favouriteService.isFavorite(userId,  bindId,type);
    }
}
